"""
1.view上面的代码
def user_list(request):
    queryset = models.Customer.objects.filter(active=1).select_related('leval')
    obj = Paginator(request, queryset)
    page_string = obj.html()

    context = {
        'queryset': queryset[obj.start:obj.end],
        'page_string': page_string
    }
    return render(request, 'user_list.html', context)
2.前端页面上的代码
<nav aria-label="Page navigation">
  <ul class="pagination">
    {{ page_string }}
  </ul>
</nav>

"""


from django.utils.safestring import mark_safe
import copy

class Paginator(object):
    def __init__(self, request, query_set, per_page=10):
        self.per_page = per_page
        self.query_set = query_set
        total_count = query_set.count()
        self.total_count = total_count
        self.total_page, count = divmod(self.total_count, self.per_page)
        if count:
            self.total_page += 1
        page = request.GET.get("page")
        if not page:
            page = 1
        else:
            if not page.isdecimal():
                page = 1
            else:
                page = int(page)
                if page < 0:
                    page = 1
                if page > self.total_page:
                    page = self.total_page
        self.page = page
        self.start = (self.page-1)*per_page
        self.end = self.page*per_page
        self.request = request


    def html(self):
        query_dict = copy.deepcopy(self.request.GET)
        query_dict._mutable = True
        if self.total_page < 11:
            start_pag = 1
            end_pag = self.total_page
        else:
            if self.page < 6:
                start_pag = 1
                end_pag = 10
            else:
                if self.page > (self.total_page - 5):
                    start_pag = self.total_page - 10
                    end_pag = self.total_page
                else:
                    start_pag = self.page-5
                    end_pag = self.page+5
        page_list = []
        query_dict.setlist("page",[1])
        page_list.append('<li ><a href="?{}">首页</a></li>'.format(query_dict.urlencode()))
        if self.page > 1:
            query_dict.setlist("page", [self.page-1])
            page_list.append('<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'.format(query_dict.urlencode()))
        for i in range(start_pag, end_pag+1):
            query_dict.setlist("page", [i])
            if i == self.page:
                page_list.append('<li class="active"><a href="?{}">{}</a></li>'.format(query_dict.urlencode(), i))
            else:
                page_list.append('<li><a href="?{}">{}</a></li>'.format(query_dict.urlencode(), i))
        if self.page < self.total_page:
            query_dict.setlist("page", [self.page+1])
            page_list.append('<li><a href="?{}" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>'.format(query_dict.urlencode()))
        query_dict.setlist("page", [self.total_page])
        page_list.append('<li ><a href="?{}">尾页</a></li>'.format(query_dict.urlencode()))
        page_string = mark_safe(''.join(page_list))
        return page_string
    def queryset(self):
        if self.total_count:
            return self.query_set[self.start:self.end]
        return self.query_set


